package com.ryskoo.catering.orderdetail.service;

import com.ryskoo.catering.orderdetail.domain.OrderDetailPO;
import com.ryskoo.catering.orderdetail.repository.IOrderDetailRepository;
import com.ryskoo.catering.ordermain.domain.OrderMainPO;
import com.ryskoo.catering.productattrvalue.domain.ProductAttrValuePO;
import com.ryskoo.catering.productattrvalue.service.IProductAttrValueService;
import com.ryskoo.catering.productmain.domain.ProductMainPO;
import com.ryskoo.catering.productmain.service.IProductMainService;
import com.ryskoo.framework.core.exception.InvalidParamException;
import com.ryskoo.framework.core.service.BasicServiceImpl;
import com.ryskoo.framework.core.utils.DoubleFormatUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @author BeHappy
 */
@Service
public class OrderDetailServiceImpl extends BasicServiceImpl<OrderDetailPO> implements IOrderDetailService {
    @Autowired
    private IOrderDetailRepository orderDetailRepository;

    @PostConstruct
    public void init() {
        this.setDao(orderDetailRepository);
    }

    @Autowired
    private IProductMainService productMainService;
    @Autowired
    private IProductAttrValueService productAttrValueService;

    @Override
    public List<OrderDetailPO> getDetailListByOrderUuid(String orderUuid) {
        return orderDetailRepository.getDetailListByOrderUuid(orderUuid);
    }

    @Override
    public void saveOrderDetail(OrderMainPO orderMain) {
        List<OrderDetailPO> detailList = orderMain.getDetailList();
        if (null != detailList) {
            double totalPrice = 0;

            //订单赠送积分
            int totalIntegral = 0;
            for (OrderDetailPO detail : detailList) {
                detail.setOrderUuid(orderMain.getUuid());
                ProductMainPO productMain = productMainService.getByUuid(detail.getProductUuid());
                if (null == productMain) {
                    //商品为空
                    throw new InvalidParamException(detail.getProductName() + ",已下架");
                }
                if (productMain.isSellOut()) {
                    //菜品已售空
                    throw new InvalidParamException(productMain.getName() + ",已售罄");
                }
                detail.setProductName(productMain.getName());
                //规格uuid为空
                if (StringUtils.isEmpty(detail.getSkuUuid())) {
                    //不参与优惠
                    if (0 == productMain.getPromotionType()) {
                        detail.setProductPrice(productMain.getPrice());
                    } else {
                        detail.setProductPrice(DoubleFormatUtil.multiply(productMain.getPrice(), productMain.getPromotionRate()));
                    }
                } else {
                    //查询规格商品
                    ProductAttrValuePO pav = productAttrValueService.getByUuid(detail.getSkuUuid());
                    if (null == pav) {
                        //规格属性为空
                        throw new InvalidParamException(detail.getProductName() + " " + detail.getShowName() + ",已下架");
                    }

                    //不参与优惠
                    if (0 == productMain.getPromotionType()) {
                        detail.setProductPrice(pav.getPrice());
                    } else {
                        double price = DoubleFormatUtil.multiply(pav.getPrice(), productMain.getPromotionRate());
                        detail.setProductPrice(price);
                    }
                }
                detail.setTotalPrice(DoubleFormatUtil.multiply(detail.getProductPrice(), detail.getBuyNum()));

                totalPrice = DoubleFormatUtil.add(totalPrice, detail.getTotalPrice());
                totalIntegral = totalIntegral + productMain.getIntegral() * detail.getBuyNum();
            }
            //判断商品价格是否正确
            if (totalPrice != orderMain.getProductPrice()) {
                throw new InvalidParamException("商品价格错误");
            }
            //判断赠送的积分是否正确
        /*if(totalIntegral != orderMain.getGiveIntegral()){
            throw new InvalidParamException("赠送积分错误");
        }*/
            this.batchCreate(detailList);
        }
    }

    @Override
    public List<String> getUuidsByProductUuid(String productUuid) {
        return orderDetailRepository.getUuidsByProductUuid(productUuid);
    }
    @Override
    public List<String> getDetailUuidsByOrderUuid(String orderUuid) {
        return orderDetailRepository.getDetailUuidsByOrderUuid(orderUuid);
    }

    @Override
    public OrderDetailPO updateState(String id, int state) {
        OrderDetailPO detailPO = this.getByUuid(id);
        detailPO.setState(state);
        this.update(detailPO);
        return detailPO;
    }
}
